<?php
die("aa");
/**
 * 例子项目采用php yaf框架
 * 请求的URL无法匹配到框架中的控制器与方式时会转发至后端服务处理
 * **/

//指向public的上一级目录
define("APP_PATH",  realpath(dirname(__FILE__) . '/../'));
date_default_timezone_set('Asia/Shanghai');

//设置配置文件
define("APP_PATH_CONFIG",APP_PATH . "/conf/application.ini");

//解析URL用于过滤是否进入框架
$url = explode("/", $_SERVER['QUERY_STRING']);
$url = ucwords(strtolower($url[0]));

if(file_exists("../application/controllers/".$url.".php") || !$_SERVER['QUERY_STRING']) //进入框架进行常规处理
{
	//运行主程序
	try
	{
		$app  = new Yaf_Application(APP_PATH_CONFIG);
		$app->bootstrap()->run();
	}
	catch(Exception $exe)
	{
		print_r($exe);die;
	}

}
else//不进入框架进行转发  不能转发 url带参数的post的方法
{
	$Config = parse_ini_file(APP_PATH_CONFIG,true);

	include '../application/library/Tools/HttpClient.php';

	//获取请求头
	$heads = array();
	foreach ($_SERVER as $key => $val)
	{
		if(substr($key,0,5) == "HTTP_")
		{
			$heads[] = strtolower(substr($key,5,strlen($key))).":".$val;
		}
	}
	$url      = $Config['product']['base_url_api_host'].$_SERVER['REQUEST_URI'];
	$client   = new Tools_HttpClient();

	if(strstr($url,"?") && explode("?",$url)[1]){//get方法
		$response = $client->request($url);
	}else{//post
		$response = $client->request($url,$_REQUEST,15,$heads);
	}
	//$response = $client->request($url,$_POST,15,$heads);

	foreach(explode("\r\n", $response['responseHead']) as $k => $val)
	{
		header($val);
	}
	echo trim($response['output']);exit();
}
?>